#ifndef __COMMON_H__
#define __COMMON_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <time.h>
#include <stdbool.h>

#define MAX_ROWS    5
#define MAX_COLS    60
#define BITMAP_SIZE (MAX_ROWS * MAX_COLS)  // 总共需要的字节数

#define SPACE_PLATE_LICENCE "        "

#define EPSILON 1e-6  // 浮点数比较的精度

#define RADAR_DATA_SIZE        2048
#define RADAR_PACKET_HEAD_SIZE 45
#define RADAR_PACKET_DATA_SIZE (RADAR_DATA_SIZE - RADAR_PACKET_HEAD_SIZE)

#define RADAR_QUEUE_SIZE         10
#define RADAR_INFO_SIZE          100
#define FALSE_TARGETS_QUEUE_SIZE 10
#define LEAVE_QUEUE_SIZE         50
#define NEW_OBJ_QUEUE_SIZE       50
#define TWIN_PAIR_QUEUE_SIZE     20
#define EVENT_STATIC_QUEUE_SIZE  50

#define IN_RADAR_VIEW_MIN_DISTANCE 200   //(100)
#define IN_RADAR_VIEW_MAX_DISTANCE 1400  //(1100)

#define OUT_RADAR_VIEW_MIN_DISTANCE 100
#define OUT_RADAR_VIEW_MAX_DISTANCE 2000
#define OUT_RADAR_MAX_DISTANCE      7500

#define POLE_DISSTANCE 10000  // 1000m

typedef struct {
    double lon;  // 经度
    double lat;  // 纬度
} coordinate_t;

typedef struct {
    int x;  // 横坐标
    int y;  // 纵坐标
} point_t;

typedef struct {
    int x;
    int y;
    int width;
    int height;
} rect_t;

typedef struct {
    int64_t time;
    int pixel_y;
} scale_t;

typedef struct {
    unsigned char data[MAX_ROWS][MAX_COLS];
    uint16_t count;
} Bitmap;

void generate_target_data_with_linear_interpolation(
    int original_data_points,  // 原始数据点数
    scale_t *original_data,    // 原始数据数组
    int target_data_points,    // 目标数据点数
    scale_t *target_data       // 目标数据数组
);

#define SWAP16(x) (x = __builtin_bswap16(x))
#define SWAP32(x) (x = __builtin_bswap32(x))
#define SWAP64(x) (x = __builtin_bswap64(x))

#define IS_BIT_SET(value, bit) (((value) >> (bit)) & 0x1)

uint32_t pro_crc32_mpeg(uint8_t *dataIn, uint32_t datalen);
void get_platform_time_stamp(unsigned char *ptime_stamp);
int64_t change_platform_time_stamp_to_milliseconds(const unsigned char *ptime_stamp);
void print_radar_timestramp(const char *radar_timestamp);
int64_t get_current_time_in_milliseconds(void);
void get_timeinfo_from_milliseconds(int64_t milliseconds, struct tm *timeinfo);
void timestamp_format_conversion(const char *radar_timestamp, char *platform_timestamp);
int64_t uuid_to_int64(const char *uuid);

void set_bit(Bitmap *bmp, int row, int col);
void clear_bit(Bitmap *bmp, int row, int col);
void clear_all_bits(Bitmap *bmp);
int get_bit(const Bitmap *bmp, int row, int col);
int get_valid_bit_count(const Bitmap *bmp);
void print_bitmap(const Bitmap *bmp);
void write_bitmap_to_file(const Bitmap *bmp, const char *filename);
char *get_bitmap_string(const Bitmap *bmp, char *buffer, size_t size);
void reverse(const void *src, void *dst, size_t len);
void *print_thread_id(void);
char *format_time_ms(int64_t ms, char *buffer, size_t buf_size);
void copy_str_with_padding(char *dest, const char *src, size_t size);
bool float_equal(double a, double b, double epsilon);
char *file_to_base64(const char *filename);
void ip_bytes_to_str(const uint8_t ip[4], char out[16]);
int hex_to_str(const uint8_t *data, size_t len, char *out, size_t out_len);
int get_time_bytes(uint8_t *buf, size_t buf_len);
double big_endian_to_double(const uint8_t *buf);
void bytes_to_hex_string(const unsigned char *data, size_t length, char *hex_string_buf, size_t buf_size);
#endif  // !__COMMON_H__
